/*
*  IGame.cpp
*  SypXIPhone
*
*  Created by Luv Khemani on 5/8/10.
*  Copyright 2010 Luv Khemani. All rights reserved.
*
*/
#include <iostream>
#include <stdlib.h>

#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include <SypX/SypX.h>

SypX::Engine* engine;
SypX::Canvas* canvas;
SypX::Renderer* render;
SypX::AudioEngine* audio;

SypX::Mesh* mesh;
SypX::MeshInstance* mi;
SypX::SpriteInstance* mi2[12];
SypX::SoundBuffer* snd;
SypX::Font* font;
SypX::Sprite* spr;

SypX::Vec4 bgColor;
SypX::Vec3 dir;


const float boxW = 50.0f;
const float boxH = 50.0f;

void handleInput()

{		
	while (true)
	{//Process all pending events until NOEVENTS is returned
		SypX::Event e = canvas->getEvent();
		switch (e.type) 
		{
		case SypX::TOUCHDOWN:
		case SypX::MOUSELEFTDOWN:
			render->setClearScreenColor(SypX::Vec4(0,0,0,1.0f));
			SypX::ErrorLogger::getLogger()<<"Down "<<e.location.x<<" "<<e.location.y<<"\n";
			break;
		case SypX::TOUCHUP:
		case SypX::MOUSELEFTUP:
			render->setClearScreenColor(SypX::Vec4(0, 1.0f, 0, 1.0f));
			SypX::ErrorLogger::getLogger()<<"Up "<<e.location.x<<" "<<e.location.y<<"\n";
			break;
		case SypX::NOEVENTS:
			return;
		default:
			break;
		}
	}

}

void run()
{		
	render->clearScreen();
	render->drawMesh(mi);

	for(int i = 0; i < 12; ++i)
	{
		mi2[i]->update();	
	}
	spr->drawBufferedInstances();

	mi->offsetPosition(dir);
	//mi->transform = glm::translate(mi->transform, dir);	
	//SypX::Vec4& pos = mi->transform[3];


	if( pos.x > canvas->getWidth() - boxW/2 || pos.x < 0 + boxW/2)
	{
		dir.x *= -1.0f;

		bgColor.r = (rand() % 10)/10.0f;
		bgColor.g = (rand() % 10)/10.0f;
		bgColor.b = (rand() % 10)/10.0f;	
		render->setClearScreenColor(bgColor);
		audio->playSound(snd, SypX::Vec3(-1.0f,0,0));
		for(int i = 0; i < 12; ++i)
		{
			mi2[i]->nextFrame();		
		}
	}
	if(pos.y > canvas->getHeight() - boxH/2 || pos.y < 0 + boxH/2)
	{
		dir.y *= -1.0f;		

		bgColor.r = (rand() % 10)/10.0f;
		bgColor.g = (rand() % 10)/10.0f;
		bgColor.b = (rand() % 10)/10.0f;
		render->setClearScreenColor(bgColor);
		audio->playSound(snd, SypX::Vec3(0.1f,0,0));
	}

	handleInput();
	canvas->swapBuffers();
}

void init()
{	
	render = engine->getRenderer();
	canvas = engine->getCanvas();
	audio = engine->getAudioEngine();

	font = engine->loadFontFromFile("Fonts/AAPEX.TTF", 24);

	mesh = new SypX::Mesh();
	mesh->initAsCircle(25.0f, 18);

	//mesh2 = new SypX::Mesh();
	//mesh2->initAsQuad(183.0f,166.0f);
	spr = new SypX::Sprite("Sprites/Icon.spr");	

	mi = new SypX::MeshInstance();
	mi->useMesh(mesh);
	mi->transform[3].x = 100.0f;
	mi->transform[3].y = 100.0f;
	mi->transform[3].z = 0.1f;

	for(int i = 0; i < 12; ++i)
	{
		mi2[i] = new SypX::SpriteInstance();
		mi2[i]->useSprite(spr);
		mi2[i]->transform[3].x = 20.0f + 20.0f* i;
		mi2[i]->transform[3].y = 240.0f;
		mi2[i]->transform[3].z = 0.0f;
		mi2[i]->setActiveAngle(i);
	}

	dir = SypX::Vec3(2.0f, 2.0f, 0);

	bgColor.r = 0.5f;
	bgColor.g = 0.5f;
	bgColor.b = 0.5f;
	bgColor.a = 1.0f;


	render->setClearScreenColor(bgColor);
	snd = engine->getAudioEngine()->loadFromWAVFile("Sounds/wave3.wav");
	
	SypX::ErrorLogger::print("Finished Loading");

}

int main(int argc, char *argv[])
{	
	SypX::Engine::create();
	engine = SypX::Engine::getSingleton();
	engine->registerInitFunction(&init);
	engine->registerRunLoop(&run);
	engine->initAndStart(320,480);		
	
	delete mi;
	for(int i = 0; i < 12; ++i)
	{
		delete mi2[i];	
	}

	engine->removeFont(font);
	SypX::Engine::destroy();

	return 0;
}
